package com.sigmazero13.bgjeeves;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.sigmazero13.bgjeeves.model.GameInfo;
import com.sigmazero13.bgjeeves.utils.GameListAdapter;

public class GameListPickerDialog extends Dialog {

	private ListView gameList;
	private List<GameInfo> games;
	private OnGamePickedListener listener;
	
	public interface OnGamePickedListener {
		public void onGamePicked(GameInfo game);
	}
	
	
	public GameListPickerDialog(Context context, List<GameInfo> games, OnGamePickedListener listener) {
		super(context);
		
		this.games = games;
		this.listener = listener;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.game_list_dialog);
		this.setTitle(R.string.game_picker_title);
		
		gameList = (ListView)findViewById(R.id.game_picker_listview);
		GameListAdapter adapter = new GameListAdapter(this.getContext(), R.layout.list_item, games);
		gameList.setAdapter(adapter);
		
		gameList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				GameInfo pickedGame = (GameInfo)parent.getItemAtPosition(position);
				
				listener.onGamePicked(pickedGame);
				GameListPickerDialog.this.dismiss();
			}
		});
	}

}
